﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;
using BUS;
namespace GUI
{
    public partial class LanXB : UserControl
    {
        public LanXB()
        {
            InitializeComponent();
        }
        private LanXBDTO _lanXB= new LanXBDTO();
        public delegate void GetString(String str);
        public GetString MyGetData;
        private bool CheckDaDien()
        {
            if (tbMaLXB.Text==""||cbMaTD.SelectedValue == null)
            {
                return false;
            }
            return true;
        }

        private void UploadData()// tao DTO
        {
            object misvalue = System.Reflection.Missing.Value;
            try
            {
                _lanXB.MaLanXB = Byte.Parse(tbMaLXB.Text);
                _lanXB.MaTuaDe = cbMaTD.Text;
                if (dUpDownDiaCD.Text == "")
                {
                    _lanXB.DiaCD = 0;
                }
                else _lanXB.DiaCD = Int32.Parse(dUpDownDiaCD.Text);
                if (tbGia.Text == "")
                {
                    _lanXB.Gia = 0;
                }
                else _lanXB.Gia = decimal.Parse(tbGia.Text);

                _lanXB.KhoGiay = cbKhoGiay.Text;

                _lanXB.MaNXB = cbMaNXB.SelectedValue.ToString();
                if (cbNamXB.Text == "")
                {
                    _lanXB.NamXB = 0;
                }
                else _lanXB.NamXB = Int32.Parse(cbNamXB.Text);
                if (tbSoLuong.Text == "")
                {
                    _lanXB.SoLuong = 0;
                }
                else _lanXB.SoLuong = Int32.Parse(tbSoLuong.Text);
                if (tbSoTrang.Text == "")
                {
                    _lanXB.SoTrang = 0;
                }
                else _lanXB.SoTrang = Int32.Parse(tbSoTrang.Text);
            }
            catch(Exception ex){}
        }

        private void LoadData()
        {
            dGVLanXb.DataSource = LanXBBUS.TimTatCaLanXB();
            cbMaNXB.DataSource = NhaXuatBanBUS.TimTatCaNXB();
            cbMaNXB.DisplayMember = "Tên NXB";
            cbMaNXB.ValueMember = "Mã NXB";

            cbMaTD.DataSource = TuaDeSachBUS.TimTatCaTuaDeSach();
            cbMaTD.DisplayMember = "Tên Tựa Đề";
            cbMaTD.ValueMember = "Mã Tựa Đề";
        }

        //Ham thiet ke
        private void LanXB_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (CheckDaDien())
            {
                if (LanXBDAO.CheckLanXBByID(Byte.Parse(tbMaLXB.Text),cbMaTD.Text))
                {
                    tbThongBao.Text = "Mã bị trùng.";
                }
                else
                {
                    UploadData();
                    if (LanXBBUS.ThemLanXB(_lanXB))
                    {
                        LoadData();
                        tbThongBao.Text = "Thêm thành công.";

                    }
                    else
                    {
                        tbThongBao.Text = "Thêm thất bại";
                    }
                }
            }
            else
            {
                tbThongBao.Text = "Bạn phải điền đầy đủ thông tin.";
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (LanXBDAO.CheckLanXBByID(Byte.Parse(tbMaLXB.Text), cbMaTD.Text))
            {
                if (LanXBBUS.XoaLanXB(Byte.Parse(tbMaLXB.Text), cbMaTD.Text))
                {
                    LoadData();
                    tbThongBao.Text = "Xóa thành công.";
                }
                else
                {
                    tbThongBao.Text = "Xóa thất bại.";
                }
            }
            else
            {
                tbThongBao.Text = "Không tìm thấy cuốn sách muốn xóa.";
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (CheckDaDien())
            {
                UploadData();
                if (LanXBBUS.SuaLanXB(_lanXB))
                {
                    tbThongBao.Text = "Sửa thành công.";
                    LoadData();
                }
                else
                {
                    tbThongBao.Text = "Sửa thất bại.";
                }
            }
            else
            {
                tbThongBao.Text = "Bạn chưa điền đầy đủ thông tin";
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
            tbSoLuong.Text = "";
            tbGia.Text = "";
            tbMaLXB.Text = "";
            tbSoTrang.Text = "";
            cbKhoGiay.Text = "";
            cbMaNXB.Text = "";
            cbMaTD.Text = "";
            cbNamXB.Text = "";
            dUpDownDiaCD.Text = "";
        }

        private void tbThongBao_TextChanged(object sender, EventArgs e)
        {
            if (MyGetData!=null)
            {
                MyGetData(tbThongBao.Text);
            }
        }

        private void dGVLanXb_SelectionChanged(object sender, EventArgs e)
        {
            if (dGVLanXb.CurrentRow!=null)
            {
                try
                {
                    tbMaLXB.Text = dGVLanXb.SelectedRows[0].Cells[0].Value.ToString();
                    cbMaTD.Text = dGVLanXb.SelectedRows[0].Cells[1].Value.ToString();
                    cbNamXB.Text = dGVLanXb.SelectedRows[0].Cells[2].Value.ToString();
                    cbKhoGiay.Text = dGVLanXb.SelectedRows[0].Cells[3].Value.ToString();
                    tbSoTrang.Text = dGVLanXb.SelectedRows[0].Cells[4].Value.ToString();
                    tbSoLuong.Text = dGVLanXb.SelectedRows[0].Cells[5].Value.ToString();
                    tbGia.Text = dGVLanXb.SelectedRows[0].Cells[6].Value.ToString();
                    dUpDownDiaCD.Text = dGVLanXb.SelectedRows[0].Cells[7].Value.ToString();
                    cbMaNXB.SelectedValue = dGVLanXb.SelectedRows[0].Cells[8].Value.ToString();

                }
                catch (Exception)
                {
                                       
                }
            }
        }

        private void tbMaLXB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void tbSoTrang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void tbSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void tbGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

        private void dUpDownDiaCD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+") == true || e.KeyChar == '\b')
                e.Handled = false;
            else
            {
                e.Handled = true;
            }
        }

      
    }
}
